// import Vue from 'vue'
// import store from '../index'
import Layout from '@/layout'
import BlankLayout from '@/layout/BlankLayout'
/* eslint-disable */
import { constantRoutes, asyncRoutes } from '@/router'

// 路由懒加载
export const loadView = (view) => {
  return (resolve) => require([`@/views/${view}.vue`], resolve)
}
// 判断Layout类型
export const isLayout = (name) => {
  const LayoutArr = ['Layout', 'BlankLayout']
  if (LayoutArr.indexOf(name) > -1) {
    if (name === 'Layout') {
      return Layout
    }
    if (name === 'BlankLayout') {
      return BlankLayout
    }
  } else {
    return false
  }
}
// 拼装路由需要的参数
export function generaMenu(routes, data) {
  data.forEach(item => {
    const menu = {
      path: item.url,
      component: isLayout(item.component) || loadView(item.component),
      hidden: item.hidden,
      redirect: item.redirect,
      children: [],
      name: item.name,
      meta: {
        title: item.meta.name,
        ...item.meta
      }
    }
    if (item.children) {
      generaMenu(menu.children, item.children)
    }
    routes.push(menu)
  })
  return routes
}

const state = {
  routes: [],
  addRoutes: [],
  defaultOpeneds: [], // 默认展开侧边栏的某些sub-menu
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes) // sidebar的数据
  },
  CLEAR_ROUTES: (state) => {
    state.addRoutes = []
    state.routes = []
  },
  SET_OPENEDS: (state, val) => {
    state.defaultOpeneds = val
  },
}

const actions = {
  // 获取权限菜单
  getPermissionMenu({ commit }) {
    // const authData = res.data.auth || [] // 按钮权限
    // Vue.ls.set('AUTHSOURCE', authData)
    var tempAsyncRoutes = []
    // var tempAsyncRoutes = Object.assign([], asyncRoutes) // 调试时放开代码注释  展示本地路由
    // const menuData = Object.assign([], res.data.menu)
    const menuDataTemp = localStorage.getItem('MENU_LIST')
    let menuData = []
    console.log('menuDataTemp', menuDataTemp)
    console.log('menuDataTemp', Object.prototype.toString.call(menuDataTemp))
    if (menuDataTemp) {
      menuData = JSON.parse(menuDataTemp).value
    }
    console.log('menuData', menuData)
    // const menuData = [
    //   {
    //     menuNo: 'string',
    //     hidden: false,
    //     meta: {
    //       icon: 'dashboard',
    //       title: '测试',
    //       noCache: true
    //     },
    //     url: '/systemCenter/userManagement/userManagement',
    //     component: 'Layout',
    //     name: 'string',
    //     children: [
    //       {
    //         url: 'userManagement',
    //         component: 'systemCenter/userManagement/userManagement',
    //         name: 'userManagement',
    //         meta: { title: '用户管理', icon: 'dashboard', noCache: true }
    //       }
    //     ]
    //   }
    // ]
    const accessedRoutes = generaMenu(tempAsyncRoutes, menuData)
    console.log('获取权限菜单', accessedRoutes)
    // 默认展开侧边栏的某些sub-menu 
    // let openeds = accessedRoutes.map((item) => item.path)
    // commit('SET_OPENEDS', openeds)
    // 404放最后 解决：动态路由刷新页面`404`问题
    accessedRoutes.push(
      { path: '*', redirect: '/404', hidden: true }
    )
    console.log('accessedRoutes', accessedRoutes)
    console.log('menuData', menuData)
    console.log('menuData.length', menuData.length)
    if (menuData.length > 0) {
      console.log('执行菜单缓存到vuex')
      commit('SET_ROUTES', accessedRoutes)
      return accessedRoutes
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
